package com.dragon.wangyiwork.controller;

import com.dragon.wangyiwork.beans.AjaxResult;
import com.dragon.wangyiwork.entity.Commodity;
import com.dragon.wangyiwork.service.CommodityService;
import com.dragon.wangyiwork.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @GetMapping("/show")
    public String viewCommodity(Integer id, Model model){
        Commodity commodity = commodityService.getCommodityById(id);
        model.addAttribute("commodity",commodity);
        return "show";
    }

    @GetMapping("/edit")
    public String editCommodity(Integer id, Model model){
        Commodity commodity = commodityService.getCommodityById(id);
        model.addAttribute("commodity",commodity);
        return "public";
    }


    @RequestMapping(value = "/account",method = {RequestMethod.GET,RequestMethod.POST})
    public String account(Model model){
        List<Commodity> commodityList = commodityService.getAccount();
        Float totalPrice = 0f;
        for(Commodity c:commodityList){
            totalPrice+=c.getBuyPrice()*c.getCount();
        }
        model.addAttribute("commodityList",commodityList);
        model.addAttribute("totalPrice",totalPrice);
        return "account";
    }

    @GetMapping("/settleAccount")
    public String settleAccount(Model model){
        List<Commodity> commodityList = commodityService.getSettleAccount();
        model.addAttribute("commodityList",commodityList);
        return "settleAccount";
    }

    @PostMapping("/publicSubmit")
    public String publicSubmit(Commodity commodity,Model model,@RequestParam("image")String pic,@RequestParam("detail")String text){
        commodity.setPic(pic);
        commodity.setText(text);
        Commodity publicCommodity = commodityService.publicCommodity(commodity);
        model.addAttribute("commodity",publicCommodity);
        return "publicSubmit";
    }

    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(@RequestParam("file") MultipartFile file){
        try {
            if (!file.isEmpty())
            {
                String filePath = FileUploadUtils.upload(file);
                return AjaxResult.success("success",filePath);
            }
            return AjaxResult.error();
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    @PostMapping("/editSubmit")
    public String editSubmit(Commodity commodity,Model model,@RequestParam("image")String pic,@RequestParam("detail")String text){
        commodity.setPic(pic);
        commodity.setText(text);
        Commodity commodity1 = commodityService.updateCommodity(commodity);
        model.addAttribute("commodity",commodity1);
        return "editSubmit";
    }

    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult deleteCommodity(Integer id){
        commodityService.deleteCommodityById(id);
        return AjaxResult.success();
    }

    @GetMapping("/public")
    public String publicPage(){
        return "public";
    }


}
